package com.company.cloud.club.modules.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.company.cloud.club.modules.constants.FileType;
import com.company.cloud.club.modules.constants.RecommendCardType;
import com.company.cloud.club.modules.core.dto.Result;
import com.company.cloud.club.modules.core.util.ValidateUtil;
import com.company.cloud.club.modules.db.dao.FileInfoMapper;
import com.company.cloud.club.modules.db.domain.*;
import com.company.cloud.club.modules.dto.IdQuery;
import com.company.cloud.club.modules.dto.LayTableResult;
import com.company.cloud.club.modules.dto.feedback.FeedbackBo;
import com.company.cloud.club.modules.dto.recommend.RecommendCardBo;
import com.company.cloud.club.modules.service.ClubFeedbackService;
import com.company.cloud.club.modules.service.RecommendTempService;
import com.company.cloud.club.modules.service.UserInfoService;
import com.company.cloud.club.modules.utils.IdGenerator;
import com.company.cloud.club.modules.utils.LoginContextUtils;
import com.company.cloud.club.modules.vo.ClubMemberVo;
import com.company.cloud.club.modules.vo.ClubVo;
import com.company.cloud.club.modules.vo.FeedbackVo;
import com.company.cloud.club.modules.vo.RecommendCardVo;
import com.company.cloud.common.auth.annotation.Login;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * *****************************************************************************
 * <p>
 * 功能名           ：com.company.cloud.club.modules.controller
 * 系统名           ：
 * <p>
 * *****************************************************************************
 * Modification History
 * <p>
 * Date        Name                    Reason for Change
 * ----------  ----------------------  -----------------------------------------
 * 2021/9/4     zhangxu                 Created
 */
@Slf4j
@RestController
@Api(tags = "RecommendTempController")
@RequestMapping("/recommend/temp")
public class RecommendTempController {

    @Autowired
    private RecommendTempService recommendTempService;

    @Autowired
    private FileInfoMapper fileInfoMapper;

    /**
     * 保存至草稿
     * @param bo
     * @return
     */
    @PostMapping(value = "add")
    public Result add(@Valid @RequestBody RecommendCardBo bo) {
        Integer currentUserId = LoginContextUtils.getUserId();

        String id = bo.getId();
        RecommendTemp card = recommendTempService.getById(id);
        if (ValidateUtil.isEmpty(card)) {
            card = new RecommendTemp(0);
        }

        card.setTitle(bo.getTitle());
        String type = bo.getType();
        card.setType(ValidateUtil.isEmpty(type) ? "评论" : type);
        card.setUserId(currentUserId);
        card.setContent(bo.getContent());
        card.setParentId(bo.getParentId());
        card.setShortContent(bo.getShortContent());
        String clubId = bo.getClubId();
        card.setClubId(ValidateUtil.isEmpty(clubId) ? "0" : clubId);
        boolean flag = recommendTempService.saveOrUpdate(card);

        fileInfoMapper.deleteByResId(card.getId());
        for (String imgUrl : bo.getImgUrls()) {
            FileInfo fileInfo = new FileInfo();
            fileInfo.setResId(card.getId());
            fileInfo.setResUrl(imgUrl);
            fileInfo.setUserId(currentUserId);
            fileInfo.setType(FileType.IMAGE);
            fileInfoMapper.insert(fileInfo);
        }

        if (!flag) {
            throw new RuntimeException("草稿保存失败");
        }

        return Result.ok(card.getId());
    }

    @PostMapping(value = "remove")
    public Result remove(@Valid @RequestBody IdQuery bo) {
        Integer currentUserId = LoginContextUtils.getUserId();
        boolean flag = recommendTempService.removeById(bo.getId());
        return Result.ok(flag);
    }

    @GetMapping(value = "detail")
    public Result detail(@Valid @ModelAttribute IdQuery bo) {
        Integer currentUserId = LoginContextUtils.getUserId();

        String id = bo.getId();
        RecommendTemp card = recommendTempService.getById(id);
        if (ValidateUtil.isEmpty(card)) {
            throw new RuntimeException("草稿不存在");
        }

        RecommendCardVo vo = new RecommendCardVo();
        vo.setTitle(card.getTitle());
        vo.setId(card.getId());
        vo.setLabel(card.getClubId());
        vo.setContent(card.getContent());
        vo.setUserId(card.getUserId());
        vo.setParentId(card.getParentId());
        vo.setType(card.getType());

        List<FileInfo> fileInfoList = fileInfoMapper.selectByResId(id);
        for (FileInfo fileInfo : fileInfoList) {
            vo.addImg(fileInfo.getUrl());
        }

        return Result.ok(vo);

    }

    @GetMapping(value = "mime")
    public Result mime() {
        Integer currentUserId = LoginContextUtils.getUserId();

        List<RecommendCardVo> list = new ArrayList<>();
        List<RecommendTemp> cardList = recommendTempService.getByUserId(currentUserId);
        for (RecommendTemp card : cardList) {

            RecommendCardVo vo = new RecommendCardVo();
            vo.setTitle(card.getTitle());
            vo.setId(card.getId());
            vo.setLabel(card.getClubId());
            vo.setContent(card.getContent());
            vo.setShortContent(card.getShortContent());
            vo.setUserId(card.getUserId());
            vo.setParentId(card.getParentId());
            vo.setType(card.getType());

            List<FileInfo> fileInfoList = fileInfoMapper.selectByResId(card.getId());
            for (FileInfo fileInfo : fileInfoList) {
                vo.addImg(fileInfo.getUrl());
            }

            list.add(vo);

        }

        return Result.ok(list);

    }

}